package com.wyd.filter;

import com.wyd.entity.Context;
import com.wyd.entity.DseProductPriorityInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Fact;
import org.jeasy.rules.annotation.Rule;

import java.util.Iterator;

@Rule(name = "available_filter", description = "产品可用阈值过滤", priority = 5)
@Slf4j
public class AvailableTypeFilter {

  @Condition
  public boolean when() {
    return true;
  }

  @Action
  public void action(@Fact("ct") Context ct) {
     log.info("开始执行>>>产品可用阈值过滤");

     if(ct.getDseProductList() == null || ct.getDseProductList().isEmpty()){
          return;
     }

     Iterator<DseProductPriorityInfo> iterator = ct.getDseProductList().iterator();
     while (iterator.hasNext()) {
         DseProductPriorityInfo info = iterator.next();
         if (ct.getAvailableTypeMap().containsKey(info.getDseProductId())) {
             Integer val = ct.getAvailableTypeMap().get(info.getDseProductId());
             if(val != null && val == 0){
                 if(info.getChannelWeightVal() < info.getAvailablePercent()) {
                     iterator.remove();
                     log.info("产品可用阈值过滤产品id>>>{},最大可用阈值：{}，当前可用阈值：{}", info.getDseProductId(), info.getChannelWeightVal(), info.getAvailablePercent());
                 }
             }

         }
     }
  }
}
